#include <stdio.h>

#define N   5

void magic(int (*a)[N])
{
    int i, j, n;

    i = 0;
    j = (N-1)/2;
    a[i][j] = 1;

    for (n = 2; n <= N*N; n++)
    {
        if (a[(i-1+N)%N][(j+1)%N] == 0)     
        {
            i = (i-1+N)%N;
            j = (j+1)%N;
            a[i][j] = n;
        }
        else
        {
            i = (i+1)%N;
            a[i][j] = n;
        }
    }

    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("%-2d ", a[i][j]);
        }
        printf("\n");
    }
}

int main(int argc, char *argv[])
{
    int array[N][N] = {{0}};
    magic(array);

    return 0;
}
